<template>
<div class="edit-main">
    <el-card class="card">
        <div slot="header">
            <el-button type="primary" v-if="id">编辑</el-button>
            <el-button type="success" v-else>新增</el-button>
        </div>
        <!--  productId,promotionPrice,promotionStartTime,promotionEndTime,promotionPerLimit,sort -->
        <el-form ref="market" :model="market" label-width="80px">
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="商品id">
                        <el-input v-model="market.productId" placeholder="请输入商品id"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="促销价格">
                        <el-input v-model="market.promotionPrice" placeholder="请输入商品促销价格"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="促销限制">
                        <el-input v-model="market.promotionPerLimit" placeholder="请输入促销限额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="开始时间">
                        <el-date-picker v-model="market.promotionStartTime" type="datetime" placeholder="请选择活动开始时间"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="结束时间">
                        <el-date-picker v-model="market.promotionEndTime" type="datetime" placeholder="请选择活动结束时间"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="排序">
                        <el-input v-model="market.sort" placeholder="请输入排序"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="24" style="text-align:center;">
                    <el-button type="primary" @click="submitForm">确定</el-button>
                </el-col>
            </el-row>
        </el-form>
        <div>

        </div>
    </el-card>
</div>
</template>

<script>
import {
    homeRecommend,
    addRecommend,
    updateRecommend
} from '@/api/market/index'

export default {
    components: {

    },
    data() {
        return {
            market: {
                productId: "",
                promotionPrice: "",
                promotionStartTime: "",
                promotionEndTime: "",
                promotionPerLimit: "",
                sort: ""
            },
            id: ''

        };
    },
    computed: {

    },
    created() {
        if (this.$route.query.id) {
            this.id = this.$route.query.id
            this.init()
        }

    },
    mounted() {

    },
    methods: {
        submitForm() {
            console.log(this.market)

            if (this.id) {
                updateRecommend(this.market).then(res => {
                    if (res.success) {
                        this.$message.success('编辑成功了')
                        this.$router.push({
                            name: 'marketList'
                        })
                    } else {
                        this.$message.error('编辑失败了!!!')
                    }
                })
            } else {
                addRecommend(this.market).then(res => {
                    if (res.success) {
                        this.$message.success('新增成功了')
                        this.$router.push({
                            name: 'marketList'
                        })
                    } else {
                        this.$message.error('新增失败了！！！')
                    }
                })
            }

        },
        init() {
            homeRecommend(this.id).then(res => {
                console.log(res);
                if (res.success) {
                    this.market=res.data.recommend
                }
            })
        }

    },
};
</script>

<style lang="scss" scoped>
.card {
    margin: 30px;
}
</style>
